package com.cskaoyan.controller.back;

import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketStorage;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.StorageImgVO;
import com.cskaoyan.config.MagicValue;
import com.cskaoyan.service.back.StorageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 对象存储
 *
 * @auther 10-何兴绕
 * @Date 2022年10月12日 11:52
 */
@RestController
@RequestMapping("/")
public class StorageController {
    @Autowired
    StorageService storageService;

    /**
     * 展示
     *
     * @param baseParam
     * @return
     */
    @RequiresPermissions("admin:storage:list")
    @GetMapping("admin/storage/list")
    public BaseRespVo showFile(BaseParam baseParam, String key, String name) {
        CommonData storageFile = storageService.getStorageFile(baseParam, key, name);
        return BaseRespVo.ok(storageFile);
    }

    /**
     * 图片上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequiresPermissions("admin:storage:create")
    @PostMapping("admin/storage/create")
    public BaseRespVo storageImg(MultipartFile file) throws IOException {

        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        String imgName = s.replace("-", "") + ".png";

        File f = new File("");
        String absolutePath = f.getAbsolutePath();
        File file1 = new File(absolutePath + "/src/main/webapp/wx/storage/fetch/" + imgName);
        file.transferTo(file1);

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(imgName);
        marketStorage.setName(file.getOriginalFilename());
        marketStorage.setType(file.getContentType());
        marketStorage.setSize((int) file.getSize());
        marketStorage.setUrl("http://localhost:8083/wx/storage/fetch/" + imgName);
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        marketStorage.setDeleted(false);
        StorageImgVO storageImgVO = storageService.storageImg(marketStorage);
        return BaseRespVo.ok(storageImgVO);
    }

    /**
     * 微信小程序图片上传
     *
     * @param file 文件
     * @return {@link BaseRespVo}
     * @throws IOException ioexception
     */
    @PostMapping("wx/storage/upload")
    public BaseRespVo WxstorageImg(MultipartFile file) throws IOException {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        String imgName = s.replace("-", "") + ".png";

        File f = new File("");
        String absolutePath = f.getAbsolutePath();
        File file1 = new File(absolutePath + "/src/main/webapp/wx/storage/fetch/" + imgName);
        file.transferTo(file1);

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(imgName);
        marketStorage.setName(file.getOriginalFilename());
        marketStorage.setType(file.getContentType());
        marketStorage.setSize((int) file.getSize());
        marketStorage.setUrl("http://localhost:8083/wx/storage/fetch/" + imgName);
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        marketStorage.setDeleted(false);
        StorageImgVO storageImgVO = storageService.storageImg(marketStorage);
        return BaseRespVo.ok(storageImgVO);
    }

    /**
     * 更新
     *
     * @param marketStorage
     * @return
     */
@RequiresPermissions("admin:storage:update")
    @PostMapping("admin/storage/update")
    public BaseRespVo updateStorage(@RequestBody MarketStorage marketStorage) {
        MarketStorage updateStorage = storageService.updateStorage(marketStorage);
        return BaseRespVo.ok(updateStorage);
    }

    /**
     * 删除
     *
     * @param marketStorage
     * @return
     */
    @RequiresPermissions("admin:storage:delete")
    @PostMapping("admin/storage/delete")
    public BaseRespVo deleteStorage(@RequestBody MarketStorage marketStorage) {
        int code = storageService.deleteStorage(marketStorage);
        return BaseRespVo.ok();
    }
}
